package com.jcl.recruit.api.mobile.controller;

import com.jcl.core.resp.ApiResponse;
import com.jcl.core.web.utils.SecurityUtils;
import com.jcl.recruit.basic.login.model.LoginResult;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.recruit.basic.login.model.UserExternalLoginInfo;
import com.jcl.recruit.mobile.service.MobileLoginService;
import com.jcl.recruit.wechat.utils.WechatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName MobileController

 * @Author yyliu
 * @Date 2018/9/19 9:17
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api")
public class MobileLoginController {

    @Autowired
    private MobileLoginService mobileLoginService;

    /* *
     * @Author yyliu
     * @Description 获取用户多个 企业登录信息
     * @Date 19:46 2018/9/12
     * @Param []
     * @return com.example.util.ApiResponse
     **/
    @RequestMapping(value = "/login/corp/change", method = RequestMethod.POST)
    public ApiResponse loginCorp(@RequestBody Map<String, String> params) {

        if(null == params || params.size() == 0){

            return ApiResponse.error(Constants.ReturnInfo.ERROR_PARAM_NULL_MSG);
        }
        String username = params.get("username");
        WechatUtils.AccessInfo accessInfo = new WechatUtils.AccessInfo();

        UserExternalLoginInfo loginInfo = mobileLoginService.changeCorpLogin(username);
        if(null == loginInfo){
            return ApiResponse.error(Constants.ReturnInfo.ERROR_PARAM_UNAUTHORIZED_MSG);
        }else{
            LoginResult result = mobileLoginService.doLogin(loginInfo.getCorpCode(),
                    loginInfo.getUserId(), SecurityUtils.FROM_WECHAT);
            accessInfo.setUserToken(result.getToken());
            accessInfo.setUserStatus(result.getUserStatus());
            accessInfo.setState("fromWechat");
        }
        return ApiResponse.success(accessInfo);
    }

}
